package cn.xjt.ints;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author xujiangtao
 * @create 2020-08-04-13:44
 */
public class tcpClient {
    public static void main(String[] args) throws IOException {
        String messageC;
        Scanner sc =new Scanner(System.in);
        //建立tcp客户端，指定ip地址，端口号
        Socket client = new Socket("192.168.137.1",8899);
        OutputStream os = client.getOutputStream();         //得到一个输出流
        InputStream inputStream = client.getInputStream();  //得到输入流
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                byte[] data = new byte[1024];
                int leng = 0;                  //读取数据
                try {
                    leng = inputStream.read(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("你接受到一条消息:"+new String(data,0,leng));    //将数据输出
            }).start();
            messageC=sc.nextLine();
            if(messageC.equals("end"))
            {
                client.close();
            }

            os.write(messageC.getBytes());                  //将字符串写入发送


        }


    }
}
